*****************************************************************************
*SET UP
*****************************************************************************
global root_dir = ""
di "$root_dir"

do "${root_dir}/Code/setup.do"

****************************************************************************************
*IMPORT FLUENCY DATA 
****************************************************************************************
forvalues i=0/37 {
    di "`i'"
    import delimited "$fluency_dir/english_fluency_`i'.csv", clear
        save "$workdir/temp_`i'", replace
}

use "$workdir/temp_0", clear
forvalues i=1/37 {
    append using "$workdir/temp_`i'"
}

duplicates drop
drop v1
bys platform_chat_id: assert _n==1
ren platform_chat_id $chatid
save "$workdir/temp_fluency", replace

forvalues i=0/37 {
    capture shell rm "$workdir/temp_`i'.dta"
}
save  "$workdir/temp_fluency", replace

****************************************************************************************
*MERGE FLUENCY INTO CHAT DATA
****************************************************************************************
use "$workdir/chat_level", clear

merge 1:1 $chatid using "$workdir/temp_fluency"
drop if _merge==2
drop _merge

gen postXevertreated=post_treat*ever_treated
save "$workdir/chat_fluency", replace

collapse fluency_score native_score, by(agent_id chat_ym)
save "$workdir/temp", replace

use "$workdir/regfile_agent_month", clear
merge 1:1 agent_id chat_ym using "$workdir/temp"
keep if _merge==3
save "$workdir/chat_fluency_ym", replace

************************************************************************************************************************
* Figure IX Panels A, B - Impact of AI on Language Skills
************************************************************************************************************************
use "$workdir/chat_fluency_ym" if agent_active==1, clear

capture drop ai_cohort
egen ai_cohort = group(ai_ym)
count if ai_cohort==.

capture drop g_*
forvalues k = 10(-1)2 {
	di "`k'"
	gen g_m`k' = rw == -`k'
	label var g_m`k' "-`k'"
}

forvalues k = 0/5 {
	gen g_`k' = rw == `k'
	label var g_`k' "`k'"
}

*Need to create a variable for t-1
gen g_m1 = 0
label var g_m1 "-1"

foreach y in fluency_score native_score {

	if "`y'" == "fluency_score" {
	    local ytext= "Change in Agent Comprehensibility Score"
	    local fname = "figureIX_panelA"
	}
	else if "`y'" == "native_score" {
	    local ytext= "Change in Agent Native Fluency Score"
	        local fname = "figureIX_panelB"
	}
	
    eventstudyinteract `y'  g_*, cohort(ai_cohort) control_cohort(never_treated) absorb(agent_id chat_ym agent_tenure_months) vce(cluster agent_id)

    forvalue i=1(1)15 {
        local m_`i'=e(b_iw)[1,`i']
        local v_`i'=e(V_iw)[`i',`i']
    }

    matrix input mat1_sa= (`m_1',`m_2',`m_3',`m_4',`m_5',`m_6',`m_7', `m_8',`m_9', 0, `m_10', `m_11', `m_12', `m_13', `m_14', `m_15')
    mat colnames mat1_sa= g_m10 g_m9 g_m8 g_m7 g_m6 g_m5 g_m4 g_m3 g_m2 g_m1 g_0 g_1 g_2 g_3 g_4 g_5

    matrix input mat3_sa= (`v_1',`v_2',`v_3',`v_4',`v_5',`v_6',`v_7', `v_8',`v_9', 0, `v_10', `v_11', `v_12', `v_13', `v_14', `v_15')
    mat colnames mat3_sa= g_m10 g_m9 g_m8 g_m7 g_m6 g_m5 g_m4 g_m3 g_m2 g_m1 g_0 g_1 g_2 g_3 g_4 g_5

    event_plot mat1_sa#mat3_sa, stub_lag(g_#) stub_lead(g_m#)  trimlag(5) trimlead(8) ciplottype(rcap) plottype(scatter) ///
		graph_opt($graphbackground xline(-0.5, lpattern(dash) lcolor(gs8)) legend(off) ///
		yline(0, lpattern(solid) lcolor(gs8)) legend(off)  ///
		xscale(r(-8.5 5.5)) xtick(-8(1)2) xlabel(-8(1)5, labsize($xlabelsize) nogrid) ///
		ytitle("`ytext'",  size($ytitlesize)) ylabel(, labsize($ylabelsize) nogrid) ///
		xtitle("Months to/from AI Deployment", size($xtitlesize)) ) ///
		lag_opt(msymbol(D) mcolor(midblue) msize(large)) together ///
		lag_ci_opt(lcolor(midblue) lwidth(medium) )
    graph export "${figures_dir}/`fname'.eps", as(eps) preview(on) replace  fontface($graphfont)
}

****************************************************************************************
*FIGURE IX Panels C, D - Impact of AI on Language Skills
****************************************************************************************
use "$dlwork/chat_fluency_ym" if agent_active==1, clear

label var native_score "Native Fluency"

label var fluency_score "Comprehensibility"

reghdfe native_score postXevertreated if agent_modecountry == "PHILLIPINES", absorb(agent_id chat_ym agent_tenure_months) cluster(agent_modeloc)
eststo P_native

reghdfe native_score postXevertreated if agent_modecountry == "US", absorb(agent_id chat_ym agent_tenure_months) cluster(agent_modeloc)
eststo US_native

reghdfe fluency_score postXevertreated if agent_modecountry == "PHILLIPINES", absorb(agent_id chat_ym agent_tenure_months) cluster(agent_modeloc)
eststo P_flu

reghdfe fluency_score postXevertreated if agent_modecountry == "US", absorb(agent_id chat_ym agent_tenure_months) cluster(agent_modeloc)
eststo US_flu


****************************************************************************************
* FIGURE IX - Impact of AI on Language Skills
* Panel C - Comprehensibility Score, by Agent Country
****************************************************************************************

coefplot (P_flu, aseq("Filipino Agents")  \ US_flu, aseq("US Agents")), ///
 keep(postXevertreated) msymbol(D) mcolor(black)  msize(large) ciopts(color(black) recast(rcap)) ///
 xlabel(.1(.05).3, ang(45) labsize($xtitlesize) nogrid) xtitle("Impact of AI Access on Comprehensibility", size($ytitlesize))  ///
 ylabel(, labsize($ytitlesize) nogrid) $graphbackground  xscale(r(.1(.05).3)) xtick(.1(.05).3) ///
 recast(scatter) swapnames
graph export "${figures_dir}/figureIX_panelC.eps", as(eps) preview(on) replace  fontface($graphfont)


****************************************************************************************
* FIGURE IX - Impact of AI on Language Skills
* Panel D - Native Fluency Score, by Agent Country
****************************************************************************************
coefplot (P_native, aseq("Filipino Agents")  \ US_native, aseq("US Agents")), ///
 keep(postXevertreated) msymbol(O) mcolor(black) msize(large) ciopts(color(black) recast(rcap)) ///
 xlabel(.1(.05).3, ang(45) labsize($xtitlesize) nogrid) xtitle("Impact of AI Access on Native Fluency", size($ytitlesize))  /// 
 ylabel(, labsize($ytitlesize) nogrid) $graphbackground xscale(r(.1(.05).3)) xtick(.1(.05).3) /// 
 recast(scatter) swapnames
graph export "${figures_dir}/figureIX_panelD.eps", as(eps) preview(on) replace  fontface($graphfont)
 
